package java6681.bank.menu;

import java6681.bank.Account6681;
import java6681.bank.CheckingAccount6681;
import java6681.bank.SavingsAccount6681;

import javax.swing.*;
import java.util.Scanner;
//javac -d . BankMenu6681.java
//java java6681.bank.menu.BankMenu6681

/**
 * @Title: BankMenu6681
 * @Author Junichiro
 * @Description:TODO
 * @Date: 2020/12/11 0011 下午 7:54
 */
public class BankMenu6681 {
    //    查询账户当前的可用额度
    private final static Account6681[] accounts = new Account6681[10];

    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "欢迎使用银行业务系统");
        menu6681();
    }

    private BankMenu6681() {

    }


    //    显示菜单项
    public static void showMenu6681() {
        System.out.println("==============212006681:张舒婷,模拟银行业务V1.0=============");
        System.out.println(" 1------------- 开设储蓄账户");
        System.out.println(" 2------------- 开设信用卡账户");
        System.out.println(" 3------------- 存款业务");
        System.out.println(" 4------------- 取款业务");
        System.out.println(" 5------------- 查询余额");
        System.out.println(" 6------------- 查询当前额度");
        System.out.println(" 0------------- 结束");
        System.out.println("===========================================");
        System.out.print("请输入您的选择：");
    }

    //    菜单的实现逻辑
    public static void menu6681() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            showMenu6681();
            boolean isFlag = true;
            while (isFlag) {
                String select = sc.nextLine().trim();
                switch (select) {
                    case "1":
                        openSavingsAccount6681();
                        isFlag = false;
                        break;
                    case "2":
                        openCheckingAccount6681();
                        isFlag = false;
                        break;
                    case "3":
                        income6681();
                        isFlag = false;
                        break;
                    case "4":
                        expenses6681();
                        isFlag = false;
                        break;
                    case "5":
                        searchBalance6681();
                        isFlag = false;
                        break;
                    case "6":
                        searchCurrentOverdraft6681();
                        isFlag = false;
                        break;
                    case "0":
                        System.out.println("谢谢使用！");
                        // System.exit(0);
                        return;
                    default:
                        System.out.println("输入错误，重新输入");
                        break;
                }
            }
            System.out.println("输入回车继续");
            sc.nextLine();
        }
    }

    //  开设储蓄账户
    public static void openSavingsAccount6681() {
        int i = getIndex();
        if (i == -1) {
            System.out.println("开户名额已用完");
        } else {
            Scanner sc = new Scanner(System.in);
            System.out.println("输入客户姓名：");
            String name = sc.nextLine();
            System.out.println("请输入开户金额:");
            double balance = sc.nextDouble();
//            double rate = 0.002;
            accounts[i] = new SavingsAccount6681(name, balance, 0.002);
            System.out.println("开户成功" + accounts[i]);
        }


    }

    //    开设信用卡账户
    public static void openCheckingAccount6681() {
        int i = getIndex();
        if (i == -1) {
            System.out.println("开户名额已用完");
        } else {
            Scanner sc = new Scanner(System.in);
            System.out.println("输入客户姓名：");
            String name = sc.nextLine();
            System.out.println("请输入申请的透支额度:");
            double overdraft = sc.nextDouble();
            accounts[i] = new CheckingAccount6681(name, 0, overdraft);
            System.out.println("开户成功" + accounts[i]);
        }
    }

    //    获得新增账户在数组中的存储位置
    public static int getIndex() {
        for (int i = 0; i < accounts.length; i++) {
            if (accounts[i] == null) {
                return i;
            }
        }
        return -1;
    }

    //    输入账户ID，查询已开户的账户对象
    public static Account6681 searchAccount() {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入账户ID:");
        String id = sc.nextLine();
        for (int i = 0; i < accounts.length; i++) {
            if (accounts[i] != null && accounts[i].getId().equals(id)) {
                return accounts[i];
            }
        }
        return null;
    }

    //    办理存款业务
    public static void income6681() {
        Account6681 account = searchAccount();
        if (account == null) {
            System.out.println("无此帐号");
            return;
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("输入存款金额：");

        double money = sc.nextDouble();
        account.deposit6681(money);
        System.out.println(account);
    }

    //    办理取款业务
    public static void expenses6681() {
        Account6681 account = searchAccount();
        if (account == null) {
            System.out.println("无此帐号");
            return;
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("输入取款金额：");
        double money = sc.nextDouble();
        account.withdraw6681(money);
        System.out.println(account);


    }

    //    查询账户的当前余额
    public static void searchBalance6681() {
        Account6681 account = searchAccount();
        if (account == null) {
            System.out.println("无此帐号");
            return;
        }
        System.out.println(account);

    }

    //    查询账户当前的可用额度
    public static void searchCurrentOverdraft6681() {
        Account6681 account = searchAccount();
        if (account == null) {
            System.out.println("无此帐号");
            return;
        }
        if (account instanceof CheckingAccount6681) {
            System.out.println("当前账户额度:" + ((CheckingAccount6681) account).getCurrentOverdraft());
        } else {
            System.out.println("当前账户不是信用卡");
        }
    }
}
